<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bouncing Ball in Rotating Hexagon</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
        }
        canvas {
            display: block;
        }
    </style>
</head>
<body>
    <canvas id="canvas"></canvas>
    <script>
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;

        // 物理参数
        const gravity = 0.2;    // 重力加速度
        const friction = 0.99;  // 摩擦系数
        
        // 图形参数
        const ballRadius = 10;     // 小球半径
        const hexagonRadius = 200; // 六边形半径
        const rotationSpeed = 0.01; // 旋转速度

        let ball = {
            x: canvas.width / 2,
            y: canvas.height / 2,
            dx: 2,
            dy: 2
        };

        let angle = 0;

        function drawHexagon(x, y, radius, rotation) {
            ctx.save();
            ctx.translate(x, y);
            ctx.rotate(rotation);
            ctx.beginPath();
            for (let i = 0; i < 6; i++) {
                const angle = (Math.PI / 3) * i;
                const x = radius * Math.cos(angle);
                const y = radius * Math.sin(angle);
                ctx.lineTo(x, y);
            }
            ctx.closePath();
            ctx.strokeStyle = '#000';
            ctx.stroke();
            ctx.restore();
        }

        function drawBall(x, y, radius) {
            ctx.beginPath();
            ctx.arc(x, y, radius, 0, Math.PI * 2);
            ctx.fillStyle = '#f00';
            ctx.fill();
            ctx.closePath();
        }

        function update() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);

            // Update ball position
            ball.dy += gravity;
            ball.x += ball.dx;
            ball.y += ball.dy;

            // Apply friction
            ball.dx *= friction;
            ball.dy *= friction;

            // Calculate distance from center
            const dx = ball.x - canvas.width / 2;
            const dy = ball.y - canvas.height / 2;
            const distance = Math.sqrt(dx * dx + dy * dy);

            // Check collision with hexagon
            if (distance + ballRadius > hexagonRadius) {
                const angle = Math.atan2(dy, dx);
                ball.x = canvas.width / 2 + (hexagonRadius - ballRadius) * Math.cos(angle);
                ball.y = canvas.height / 2 + (hexagonRadius - ballRadius) * Math.sin(angle);

                // Reflect velocity
                const normalX = Math.cos(angle);
                const normalY = Math.sin(angle);
                const dot = ball.dx * normalX + ball.dy * normalY;
                ball.dx -= 2 * dot * normalX;
                ball.dy -= 2 * dot * normalY;
            }

            // Draw hexagon
            drawHexagon(canvas.width / 2, canvas.height / 2, hexagonRadius, angle);

            // Draw ball
            drawBall(ball.x, ball.y, ballRadius);

            // Update hexagon rotation
            angle += rotationSpeed;

            requestAnimationFrame(update);
        }

        update();
    </script>
</body>
</html>